package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.system.domain.ReplyTemplate;
import com.xhh.nphm.system.service.IReplyTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 回复内容模板管理层
 */
@RestController
@RequestMapping("/system/replyTemplate")
public class ReplyTemplateController extends BaseController {
    private String prefix = "system/replyTemplate";
    @Autowired
    IReplyTemplateService replyTemplateService;

    /**
     * 打开回复模板内容页面
     * @return
     */
    @GetMapping("/replyTemplateList")
    public ModelAndView rTempList(){
        return new ModelAndView(prefix+"/replyTemplateList");
    }

    @PostMapping("/replyTemplateListJson")
    @ResponseBody
    public TableDataInfo rTempListJson(ReplyTemplate replyTemplate) {
        startPage();
        List<Map<String, Object>> list = replyTemplateService.selectReplyListJsonByDept(getSysUser().getDeptId().toString(),replyTemplate);
        return getDataTable(list);
    }

    /**
     * 加载编辑页面
     * @param PkId
     * @param map
     * @return
     */
    @GetMapping("/rTempEdit/{PkId}")
    public ModelAndView editReplyTemplate(@PathVariable("PkId") Integer PkId, ModelMap map){
        map.put("replyTemplate",replyTemplateService.selectByPkId(PkId));
        return new ModelAndView(prefix+"/replyTemplateEdit");
    }

    /**
     * 保存编辑页面
     * @return
     */
    @PostMapping("/rTempEdit")
    @ResponseBody
    public AjaxResult updateReplyTemplate(ReplyTemplate replyTemplate){
        SysUser user = getSysUser();
        replyTemplate.setOptUserId(user.getUserId());
        replyTemplate.setOptName(user.getUserName());
        replyTemplate.setIsDel(0);
        replyTemplate.setOptTime(new Date());
        String oldStr = replyTemplate.getReplyContent();
        if(oldStr != null){
            replyTemplate.setReplyContent(oldStr.replace(removeStr,""));
        }
        if (replyTemplateService.updateReplyTemplate(replyTemplate)) {
            return success("修改成功");
        }else {
            return error("修改失败");
        }

    }


    /**
     * 添加回复模板页面
     * @return
     */
    @GetMapping("/rTempAdd")
    public ModelAndView addReplyTemplate(){
        return new ModelAndView(prefix+"/replyTemplateAdd");
    }

    /**
     * 保存回复模板
     * @return
     */
    @PostMapping("/rTempAdd")
    @ResponseBody
    public AjaxResult saveReplyTemplate(ReplyTemplate replyTemplate){
        SysUser user = getSysUser();
        replyTemplate.setDeptId(user.getDeptId());
        replyTemplate.setDeptName(user.getDeptName());
        replyTemplate.setOptUserId(user.getUserId());
        replyTemplate.setOptName(user.getUserName());
        replyTemplate.setIsDel(0);
        replyTemplate.setOptTime(new Date());
        String oldStr = replyTemplate.getReplyContent();
        if(oldStr != null){
            replyTemplate.setReplyContent(oldStr.replace(removeStr,""));
        }
        if (replyTemplateService.saveReplyTemplate(replyTemplate)) {
            return success("添加成功");
        }else{
            return error("添加失败");
        }
    }

    @PostMapping("/rTempRemove/{pkId}")
    @ResponseBody
    public AjaxResult rTempRemove(@PathVariable String pkId){
        return toAjax(replyTemplateService.rTempRemove(pkId));
    }
}
